﻿using System;
using System.Data;
using System.Collections.Generic;
using Slja.Model;
using System.Text.RegularExpressions;
namespace Slja.BLL
{
	/// <summary>
	/// LinkMan
	/// </summary>
	public partial class LinkManBLL
	{
        
        private readonly Slja.DAL.LinkManDAL dal = new Slja.DAL.LinkManDAL();
        public LinkManBLL()
		{}
		#region  Method
        public string GetLinkManFromKeyWord(string keyword,LinkManType lmType)
        {            
            return GetLinkManFromKeyWord(keyword, lmType, LinkManInfoClass.lkmUserID,100,1);
        }
        public string GetLinkManFromKeyWord(string keyword, LinkManType lmType, LinkManInfoClass infoCls, int curPage)
        {
            return GetLinkManFromKeyWord(keyword, lmType, infoCls, 5, curPage);
        }
        public string GetLinkManFromKeyWord(string keyword, LinkManType lmType, LinkManInfoClass infoCls,int pageSize,int curPage)
        {
            string strXml = "";
            int pagecount=0 ;
            if (keyword.Length >= 2 || keyword.Length == 0)
            {
                DataTable dt = GetLinkManByKeyword(keyword, lmType, pageSize, curPage, out pagecount);
                if (infoCls == LinkManInfoClass.lkmUserID)
                {
                    strXml = BLLCommon.BuildXmlData(dt, "ID", "FullName", "{0}*{1}", "{1}", pagecount);
                }
                else if (infoCls == LinkManInfoClass.lkmMobileTel)
                {
                    strXml = BLLCommon.BuildXmlData(dt, "FullName", "MobileTel", "{1}", "{0}[{1}]", pagecount);
                }
            }           
            return strXml;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <param name="lmType">联系人类型</param>
        /// <param name="pageSize">页大小(0表使用系统默认值)</param>
        /// <param name="curPage">当前页号</param>
        /// <param name="PageCount">返回总页数</param>
        /// <returns>数据表</returns>
        public DataTable GetLinkManByKeyword(string keyword, LinkManType lmType,int pageSize, int curPage, out int PageCount)
        {
            Regex regChz2 = new Regex(@"[\u0391-\uFFE5]{2,}");//2个以上的汉字
            Regex regEng2 = new Regex(@"[0-9,A-Z,a-z]{2,}");//2个以上的拼音字母


            string strWhere = " 1=1 ";
            if (lmType != LinkManType.None)
            {
                strWhere +=" and [Type]='" + ((int)lmType).ToString() + "'";
            }            
            if (regChz2.IsMatch(keyword))
            {
                strWhere += " and FullName like '" + keyword + "%'";
            }
            if (regEng2.IsMatch(keyword))
            {
                strWhere += " and (Pinyin like '" + keyword + "%' OR ShortPy like '" + keyword + "%')";
            }


            //ClientDAL dal = new RegionCity();
            int recordCount;
            if (pageSize <= 0) {pageSize= BLLCommon._PageSize;}
            DataSet ds = dal.GetList(strWhere, "Pinyin Asc", pageSize, curPage, out recordCount);
            PageCount = Convert.ToInt32(Math.Ceiling(recordCount * 1.0d / pageSize));
            return ds.Tables[0];        
        }
		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			return dal.Exists(ID);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int  Add(Slja.Model.LinkManModel model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Slja.Model.LinkManModel model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			return dal.Delete(ID);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			return dal.DeleteList(IDlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Slja.Model.LinkManModel GetModel(int ID)
		{
			
			return dal.GetModel(ID);
		}

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
		/// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Slja.Model.LinkManModel> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        /// <param name="strWhere">where条件</param>
        /// <param name="strWhere">是否包含系统用户</param>
        public List<Slja.Model.LinkManModel> GetModelList(string strWhere, bool IsContainSysUser)
        {
            DataSet ds = dal.GetList(strWhere,IsContainSysUser);
            return DataTableToList(ds.Tables[0]);
        }
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Slja.Model.LinkManModel> DataTableToList(DataTable dt)
		{
			List<Slja.Model.LinkManModel> modelList = new List<Slja.Model.LinkManModel>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Slja.Model.LinkManModel model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new Slja.Model.LinkManModel();
                    dal.AssignModel(dt.Rows[n], model);					
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}
		#endregion  Method
	}
}

